package userServlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import userDao.UserDao;
import userEntity.User;
import userImplment.UserImplement;

/**
 * Servlet implementation class AddUser
 */
public class AddUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String Uname = request.getParameter("Uname");
		String passWord = request.getParameter("passWord");
		String idType = request.getParameter("idType");
		String idCard = request.getParameter("idCard");
		String phone = request.getParameter("phone");
		//将表单获得的用户注册信息封装为一个User对象
		User user=new User();
		user.setUname(Uname);
		user.setPassWord(passWord);
		user.setIdType(idType);
		user.setIdCard(idCard);
		user.setPhone(phone);
		
		UserDao userImplement=new UserImplement();
		
			if(userImplement.AddUser(user)) {
				//插入成功设置session，并跳转到个人中心首页
				HttpSession session = request.getSession();
				 user.setId(userImplement.Query(Uname).getId());
				session.setAttribute("user", user);
				request.getRequestDispatcher("my.jsp").forward(request, response);
			}else {
				//插入失败跳转到出错提示页面
				request.getRequestDispatcher("myAddOrror.jsp").forward(request, response);
			}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
